﻿// Copyright (c) 2012-2013 fancidev
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Util.Media
{
    public enum ColorSpace
    {
        RGB = 0,
        HSB = 1,
        CIEXYZ = 2,
        CIELAB = 3,
    }

    public struct ColorCoordinates
    {
        public float X;
        public float Y;
        public float Z;

        public static ColorCoordinates FromColor(Color c)
        {
            ColorCoordinates cc;
            cc.X = c.R;
            cc.Y = c.G;
            cc.Z = c.B;
            return cc;
        }

        public static Color ToColor(ColorCoordinates cc)
        {
            return Color.FromArgb(
                (byte)Math.Round(cc.X),
                (byte)Math.Round(cc.Y),
                (byte)Math.Round(cc.Z));
        }

        public static ColorCoordinates GetWeightedAverage(
            ColorCoordinates c1, float w1, ColorCoordinates c2, float w2)
        {
            return new ColorCoordinates
            {
                X = (c1.X * w1 + c2.X * w2) / (w1 + w2),
                Y = (c1.Y * w1 + c2.Y * w2) / (w1 + w2),
                Z = (c1.Z * w1 + c2.Z * w2) / (w1 + w2),
            };
        }

        public static float DistanceSquared(
            ColorCoordinates c1, ColorCoordinates c2)
        {
            return (c1.X - c2.X) * (c1.X - c2.X)
                 + (c1.Y - c2.Y) * (c1.Y - c2.Y)
                 + (c1.Z - c2.Z) * (c1.Z - c2.Z);
        }
    }
}
